package com.vick.tool;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.gen.com.GameRoomManagerActivity;
import com.gen.com.GameRoomPlayerActivity;
import com.gen.tool.PlayerInfo;
import com.ouken.tool.MyApp;

public class MiscTool {
	public static void goToRoom(Context activityNow, String managerEmail) {
		Intent intent;
		MyApp myApp = (MyApp) activityNow.getApplicationContext();

		if (managerEmail != null) {
			if (managerEmail.equals(myApp.getUserEmail())) {
				intent = new Intent(activityNow, GameRoomManagerActivity.class);
			} else {
				intent = new Intent(activityNow, GameRoomPlayerActivity.class);
			}
		} else {

			ArrayList<PlayerInfo> playerList = myApp.getPlayerList();
			if ("4".equals(playerList.get(0).getState())) {
				intent = new Intent(activityNow, GameRoomManagerActivity.class);
			} else {
				intent = new Intent(activityNow, GameRoomPlayerActivity.class);
			}
		}

		activityNow.startActivity(intent);
		((Activity) activityNow).finish();
		// DialogTool.showCustomMessage(activityNow, "Go to room");
	}

	public static void showAppError(Context activityNow, String errorMessage) {
		new AlertDialog.Builder(activityNow).setTitle("Error")
				.setMessage("Found a BUG!!!!").setCancelable(false)
				.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				}).create().show();
		System.out.println("ERROR: " + errorMessage);
	}

	public static PlayerInfo getProvider(Context activityNow) {
		MyApp myApp = (MyApp) activityNow.getApplicationContext();
		ArrayList<PlayerInfo> players = myApp.getPlayerList();
		for (PlayerInfo player : players) {
			if ("provider".equals(player.getRole())) {
				return player;
			}
		}
		showAppError(activityNow, "Provider not found");
		System.out.println("provider not found");
		return null;
	}

	public static void setMyAppQuitRoom(Context activityNow) {
		// MyApp myApp = (MyApp) activityNow.getApplicationContext();
		// myApp.setRoomName(null);
		// myApp.setMem(null, null);
		// myApp.setPlayerList(null);
	}

}
